package com.net.lnk.batch.bank.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.net.lnk.batch.bank.dao.BankcardMapper;
import com.net.lnk.batch.bank.dto.BankcardDTO;
import com.net.lnk.batch.bank.dto.BankcardPageableDTO;
import com.net.lnk.batch.bank.model.BankcardModel;

import tk.mybatis.mapper.entity.Example;

@RestController
@RequestMapping("/bankcard")
public class BankcardController {

	@Autowired
	private BankcardMapper bankcardMapper;

	@GetMapping("/{bin}")
	public BankcardDTO getBankcard(@PathVariable("bin") String bin) {
		BankcardModel bankcard = new BankcardModel();
		bankcard.setBin(bin);
		bankcard = bankcardMapper.selectOne(bankcard);
		if (bankcard != null) {
			BankcardDTO bankcardDTO = new BankcardDTO();
			bankcardDTO.setBankName(bankcard.getBankName());
			bankcardDTO.setCardName(bankcard.getCardName());
			bankcardDTO.setBinLength(bankcard.getBinLength());
			bankcardDTO.setBin(bankcard.getBin());
			bankcardDTO.setCardNoLength(bankcard.getCardNoLength());
			bankcardDTO.setCardNoSample(bankcard.getCardNoSample());
			bankcardDTO.setCardType(bankcard.getCardType());
			return bankcardDTO;
		}

		return null;
	}

	@GetMapping("/list")
	public List<BankcardDTO> getBankcardList(String bankCode) {
		Example cond = new Example(BankcardModel.class);
		cond.createCriteria().andEqualTo("bankCode", bankCode);
		cond.setOrderByClause("bin asc");
		List<BankcardModel> bankcardList = bankcardMapper.selectByExample(cond);
		List<BankcardDTO> dtoList = new ArrayList<BankcardDTO>();
		for (BankcardModel bankcard : bankcardList) {
			BankcardDTO bankcardDTO = new BankcardDTO();
			bankcardDTO.setBankName(bankcard.getBankName());
			bankcardDTO.setCardName(bankcard.getCardName());
			bankcardDTO.setBinLength(bankcard.getBinLength());
			bankcardDTO.setBin(bankcard.getBin());
			bankcardDTO.setCardNoLength(bankcard.getCardNoLength());
			bankcardDTO.setCardNoSample(bankcard.getCardNoSample());
			bankcardDTO.setCardType(bankcard.getCardType());
			dtoList.add(bankcardDTO);
		}

		return dtoList;
	}

	@GetMapping("/pageable")
	public BankcardPageableDTO pageableBankcard(String bankCode, int pageNum, int pageSize) {
		BankcardModel cond = new BankcardModel();
		cond.setBankCode(bankCode);
		PageHelper.startPage(pageNum, pageSize, Boolean.TRUE);
		Page<BankcardModel> pageInfo = (Page<BankcardModel>) bankcardMapper.select(cond);
		BankcardPageableDTO pageDTO = new BankcardPageableDTO();
		pageDTO.setCount(pageInfo.getTotal());
		pageDTO.setPageNum(pageInfo.getPageNum());
		pageDTO.setPageSize(pageInfo.getPageSize());
		List<BankcardDTO> bankcardList = new ArrayList<BankcardDTO>();
		for (BankcardModel bankcard : pageInfo) {
			BankcardDTO bankcardDTO = new BankcardDTO();
			bankcardDTO.setBankName(bankcard.getBankName());
			bankcardDTO.setCardName(bankcard.getCardName());
			bankcardDTO.setBinLength(bankcard.getBinLength());
			bankcardDTO.setBin(bankcard.getBin());
			bankcardDTO.setCardNoLength(bankcard.getCardNoLength());
			bankcardDTO.setCardNoSample(bankcard.getCardNoSample());
			bankcardDTO.setCardType(bankcard.getCardType());
			bankcardList.add(bankcardDTO);
		}
		pageDTO.setBankcardList(bankcardList);

		return pageDTO;
	}

}
